package com.bys.admin.system.service.impl;

import com.bys.admin.system.entity.SysDataResource;
import com.bys.admin.system.service.ISysDataResourceService;
import com.bys.admin.system.mapper.SysDataResourceMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 数据源配置 服务实现类
 * </p>
 *
 * @author bys
 * @since 2025-02-20
 */
@Service
public class SysDataResourceServiceImpl extends ServiceImpl<SysDataResourceMapper, SysDataResource> implements ISysDataResourceService {

    @Resource
    private DataSourceProperties dataSourceProperties;

    @Override
    public List<SysDataResource> getDataResourceList() {
        List<SysDataResource> list = list();
        list.add(0, buildMasterDataSource());
        return list;
    }

    @Override
    public SysDataResource getDataSourceConfig(Long dataResourceId) {
        if(Objects.isNull(dataResourceId) || SysDataResource.ID_MASTER.equals(dataResourceId)){
            return buildMasterDataSource();
        }else{
            return getById(dataResourceId);
        }


    }

    private SysDataResource buildMasterDataSource() {
        SysDataResource sysDataResource = SysDataResource.builder().name("花海数据库").url(dataSourceProperties.getUrl()).username(dataSourceProperties.getUsername()).password(dataSourceProperties.getPassword()).build();
        sysDataResource.setId(SysDataResource.ID_MASTER);
        return sysDataResource;
    }
}
